Test Failed
Push — master ( db6889...9321a0 )
by Dmytro
01:41
created

utils.js ➔ resolve   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
1
import path from 'path';
2
import { assert } from 'chai';
3
import { entry } from './constants';
4
5
export function load(relPath, clearCache) {
6
    const absPath = path.resolve(entry, relPath);
7
8
    if (clearCache) delete require.cache[require.resolve(absPath)];
9
    // eslint-disable-next-line security/detect-non-literal-require
10
    const result =  require(absPath);
11
12
    if (clearCache) delete require.cache[require.resolve(absPath)];
13
14
    return result;
15
}
16
17
export function resolve(relPath) {
18
    return require.resolve(path.join(entry, relPath));
19
}
20
21
export class RuleTester {
22
    constructor(rules) {
23
        const { default:cottus, ValidationError } = load('index.js');
24
25
        this._validator = cottus.compile(rules);
26
        this._ValidationError = ValidationError;
27
    }
28
29
    positive(input, output) {
30
        const result = this._validator.validate(input);
31
32
        assert.deepEqual(result, output);
33
    }
34
35
    negative(input, code, message) {
36
        try {
37
            this._validator.validate(input);
38
            assert.fail('rule should fail');
39
        } catch (error) {
40
            if (!(error instanceof this._ValidationError)) throw error;
41
            const json = error.toJSON();
42
43
            assert.deepEqual(json.details[0], {
44
                code,
45
                message,
46
                value : input
47
            });
48
        }
49
    }
50
}
51